/**
 * 后台转账入口
 */
package com.p2p.backend.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.backend.controller.form.TransForm;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.exception.BizException;
import com.p2p.base.model.Money;
import com.p2p.base.web.controller.BaseController;
import com.p2p.trans.client.enums.SubTransCodeEnum;
import com.p2p.trans.client.enums.TransCodeEnum;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.model.TransInfo;
import com.p2p.trans.client.result.AccountTransResult;

/**
 * @author ruiqi
 *
 */
@RequestMapping("/trans")
@Controller
public class TransController extends BaseController{

	@Resource
	AccountTransFacade remoteAccountTransFacade;
	
	@PermissionNeed("accountmanual")
	@RequestMapping("/index")
	public String index(){
		return "trans/index";
	}
	
	@PermissionNeed("accountmanual_trans")
	@RequestMapping(value="/doubleTrans",params="action=doTrans")
	public String doubleTran(@Valid TransForm transForm,Errors errors,Model map){
		
		if(errors.hasErrors()){
			map.addAttribute("errors",errors.getAllErrors());
			return "trans/doubleTransIndex";
		}
		TransInfo transInfo = new TransInfo();
		transInfo.setTransAccountIn(transForm.getAccountNoIn());
		transInfo.setTransAccountOut(transForm.getAccountNoOut());
		transInfo.setTransAmount(new Money(transForm.getAmount()));
		transInfo.setTransCode(TransCodeEnum.TXCODE_TRANSFER_MANUAL);
		
		String memo = transForm.getMemo();
		if(StringUtils.isBlank(memo)){
			memo = "人工补账";
		}
		transInfo.setTransMemo(memo);
		AccountTransResult result = remoteAccountTransFacade.transfer(transInfo);
		if(result.isSuccess()){
			map.addAttribute("res", 1);
		}else{
			map.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			if(StringUtils.isNotBlank(msg)){
				map.addAttribute("msg",msg);
			}else{
				map.addAttribute("msg",code );
			}
			
			
		}
		return "trans/douleTransIndex";
	}
	
	@PermissionNeed("accountmanual_trans")
	@RequestMapping(value="/doubleTrans",params="action=index")
	public String doubleTranIndex(TransForm transForm,ModelMap map){
		map.addAttribute("transForm", transForm);
		return "trans/doubleTransIndex";
	}
	
	@PermissionNeed("accountmanual_trans")
	@RequestMapping(value="/doubleTrans",params="action=accountIn")
	public String doubleTransIn(String accountNo,ModelMap map){
		TransForm transForm = new TransForm();
		transForm.setAccountNoIn(accountNo);
		return doubleTranIndex(transForm,map);
	}
	
	@PermissionNeed("accountmanual_trans")
	@RequestMapping(value="/doubleTrans",params="action=accountOut")
	public String doubleTransOut(String accountNo,ModelMap map){
		TransForm transForm = new TransForm();
		transForm.setAccountNoOut(accountNo);
		return doubleTranIndex(transForm,map);
	}
	
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doDeposit", params="action=doTrans")
	public String doDeposit(@Valid TransForm transForm,Errors errors,ModelMap map){
		if(errors.hasErrors()){
			map.addAttribute("errors",errors.getAllErrors());
			return "trans/deposit";
		}
		TransInfo transInfo = new TransInfo();
		transInfo.setTransAccountIn(transForm.getAccountNoIn());
		transInfo.setTransAccountOut(transForm.getAccountNoOut());
		transInfo.setTransAmount(new Money(transForm.getAmount()));
		transInfo.setTransCode(TransCodeEnum.TXCODE_DEPOSIT);
		transInfo.setSubTransCode(SubTransCodeEnum.SUB_DEPOSIT_MANUAL);
		String memo = transForm.getMemo();
		if(StringUtils.isBlank(memo)){
			memo = "人工充值";
		}
		transInfo.setTransMemo(memo);
		AccountTransResult result = remoteAccountTransFacade.doDeposit(transInfo);
		if(result.isSuccess()){
			map.addAttribute("res", 1);
		}else{
			map.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			if(StringUtils.isNotBlank(msg)){
				map.addAttribute("msg",msg);
			}else{
				map.addAttribute("msg",code );
			}
		}
		return "trans/deposit";
	}
	
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doDeposit",params="action=index")
	public String doDepositIndex(TransForm transForm,ModelMap map){
		map.addAttribute("transForm", transForm);
		transForm.setAccountNoOut(InnerAccountConsts.innerTotalAccountNo);
		return "trans/deposit";
	}
	
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doDeposit",params="action=accountIn")
	public String doDepositIndex(String accountNo,ModelMap map){
		TransForm transForm = new TransForm();
		transForm.setAccountNoIn(accountNo);
		return doDepositIndex(transForm,map);
	}
	
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doWithdraw", params="action=doTrans")
	public String doWithdraw(@Valid TransForm transForm,Errors errors,ModelMap map){
		if(errors.hasErrors()){
			map.addAttribute("errors",errors.getAllErrors());
			return "trans/withdraw";
		}
		TransInfo transInfo = new TransInfo();
		transInfo.setTransAccountIn(transForm.getAccountNoIn());
		transInfo.setTransAccountOut(transForm.getAccountNoOut());
		transInfo.setTransAmount(new Money(transForm.getAmount()));
		transInfo.setTransCode(TransCodeEnum.TXCODE_WITHDRAW);
		transInfo.setSubTransCode(SubTransCodeEnum.SUB_WITHDRAW_MANUAL);
		String memo = transForm.getMemo();
		if(StringUtils.isBlank(memo)){
			memo = "人工提现补账";
		}
		transInfo.setTransMemo(memo);
		AccountTransResult result = remoteAccountTransFacade.doWithdraw(transInfo);
		if(result.isSuccess()){
			map.addAttribute("res", 1);
		}else{
			map.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			if(StringUtils.isNotBlank(msg)){
				map.addAttribute("msg",msg);
			}else{
				map.addAttribute("msg",code );
			}
		}
		return "trans/withdraw";
	}
	
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doWithdraw", params="action=index")
	public String doWithdrawIndex(TransForm transForm,ModelMap map){
		map.addAttribute("transForm", transForm);
		transForm.setAccountNoIn(InnerAccountConsts.innerTotalAccountNo);
		return "trans/withdraw";
	}
	@PermissionNeed("accountmanual_withdraw")
	@RequestMapping(value="/doWithdraw", params="action=accountOut")
	public String doWithdrawIndex(String accountNo,ModelMap map){
		TransForm transForm = new TransForm();
		transForm.setAccountNoOut(accountNo);
		return doWithdrawIndex(transForm,map);
	}
	
}
